function Dog(name,energy){
  let _listeners = {
  // 'event1': [listener1,listener2, ...],
  // 'event2': []
  };

  let _name   = name,
      _energy = energy;

  let timer = setInterval(() => {
    if(energy > 0) {
      emit('bark');
      _energy--;                    
    }
    if(_energy<0){
      clearInterval(timer);            
    }   
  }, 1000);

  this.name   = () => _name;
  this.energy = () => _energy;
  this.on = (evt,fn)=>{
  //validate  校验_listeners[evt] exist (是存在的)
    if(typeof(_listeners[evt])==='undefined'){
      _listeners[evt] = [];
    }
    _listeners[evt].push(fn);
  };

  function emit(evt, arg) {
    if(typeof(_listeners[evt]) === 'undefined') {
      return;                 
    }

    _listeners[evt].forEach((fn) => {
      fn.call(this, arg);                
    });        
  }
}

module.exports = Dog;

